#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

/// Definition for the callback function that reports the result of an meta operation.
///
/// # Arguments
///
/// * `context` - A pointer to user-defined data, passed from the original function call.
/// * `error_code` - An integer indicating the status of the operation. `0` for success.
/// * `error_desc` - A C-string with a description of the error.
/// * `meta_content` - A C-string with the resulting metadata content, if successful.
///
/// # Safety
///
/// The `error_desc` and `meta_content` pointers are managed by Rust and must not be freed by the caller.
/// If the data is needed after the callback returns, it must be copied.
using MetaCallback = void(*)(void *context,
                             int64_t error_code,
                             const char *error_desc,
                             const char *meta_content);

extern "C" {

/// Reads AIGC metadata from a JPEG file.
///
/// This function is synchronous and uses a callback to return the result.
///
/// # Arguments
///
/// * `asset_path` - A pointer to a null-terminated UTF-8 string representing the path to the source JPEG file.
/// * `context` - A pointer to user-defined data that will be passed to the callback.
/// * `callback` - A function pointer to be called with the result.
///
/// # Returns
///
/// Returns `true` if the operation was successfully initiated, `false` otherwise. The actual success or failure of the read operation is reported via the callback.
///
/// # Safety
///
/// The caller must ensure that `asset_path` is a valid pointer to a null-terminated UTF-8 encoded string.
/// The `context` pointer must also be valid. The pointers must be valid for the duration of the function call.
/// The caller is responsible for managing the memory of the pointers it passes to this function.
bool read_meta(const char *asset_path,
               void *context,
               MetaCallback callback);

/// Writes AIGC metadata to a JPEG file.
///
/// This function is synchronous and uses a callback to return the result.
///
/// # Arguments
///
/// * `input_path` - A pointer to a null-terminated UTF-8 string representing the path to the source JPEG file.
/// * `output_path` - A pointer to a null-terminated UTF-8 string representing the path where the output JPEG file will be saved.
/// * `content_provider` - A pointer to a null-terminated UTF-8 string for the AIGC content_producer|content_propagator.
/// * `label` - A pointer to a null-terminated UTF-8 string for the AIGC label.
/// * `provider_id` - A pointer to a null-terminated UTF-8 string for the AIGC produce_id|propagate_id.
/// * `write_as_producer` - A boolean flag. If true, writes metadata as a producer; otherwise, as a propagator.
/// * `context` - A pointer to user-defined data that will be passed to the callback.
/// * `callback` - A function pointer to be called with the result.
///
/// # Returns
///
/// Returns `true` if the operation was successfully initiated, `false` otherwise. The actual success or failure of the write operation is reported via the callback.
///
/// # Safety
///
/// The caller must ensure that `input_path`, `output_path`, `content_provider`, `label`, and `provider_id` are valid pointers to null-terminated UTF-8 encoded strings.
/// The `context` pointer must also be valid. The pointers must be valid for the duration of the function call.
/// The caller is responsible for managing the memory of the pointers it passes to this function.
bool write_meta(const char *input_path,
                const char *output_path,
                const char *content_provider,
                const char *label,
                const char *provider_id,
                bool write_as_producer,
                void *context,
                MetaCallback callback);

}  // extern "C"
